package cn.rongdao.jrkabao.ui.main.mainfragment.uicomponent;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.http.request.callback.BaseImpl;
import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.deposit.AccountInfoBean;
import cn.rongdao.jrkabao.dao.UserDao;
import cn.rongdao.jrkabao.model.main.personal.PersonUserInfoModel;
import cn.rongdao.jrkabao.ui.deposit.account.MyAccountActivity;
import cn.rongdao.jrkabao.ui.main.mainfragment.personal.PersonInfoActivity;
import cn.rongdao.jrkabao.ui.main.mainfragment.vip.MemberActivity;
import cn.rongdao.jrkabao.ui.main.mainfragment.vip.MyMemberActivity;
import cn.rongdao.jrkabao.ui.setting.SettingActivity;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.LoginUtil;

/**
 * Created by jack on 17/5/17
 */

public class PersonUserInfoUIComponent extends LinearLayout implements View.OnClickListener {

    public PersonUserInfoUIComponent(Context context) {
        super(context);
    }

    public PersonUserInfoUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public PersonUserInfoUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private TitleBarUIComponent titleBarUIComponent;

    private TextView tvPhone;
    private ImageView ivAvatar;
    private LinearLayout llAccount;
    private RelativeLayout rlUserInfo;
    private TextView tvReturnAccount;
    private TextView tvCashCoupon;
    private FrameLayout flMember;
    private TextView tvMember;
    private ImageView ivVip;
    private RelativeLayout rlAvatar;
    private View viewLine;

    private boolean isPersonal;
    private boolean isMember;
    private boolean memberOverdue;

    private AccountInfoBean accountInfoBean;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initView();
    }

    private void initView() {
        titleBarUIComponent = findViewById(R.id.fp_titlebar);
        tvPhone = findViewById(R.id.fp_tv_phone);
        ivAvatar = findViewById(R.id.fp_iv_avatar);
        rlUserInfo = findViewById(R.id.fp_rl_user_info);
        llAccount = findViewById(R.id.fp_ll_account);
        tvReturnAccount = findViewById(R.id.fp_tv_return_account);
        tvCashCoupon = findViewById(R.id.fp_tv_cash_coupon);
        flMember = findViewById(R.id.fp_fl_member);
        tvMember = findViewById(R.id.fp_tv_member);
        ivVip = findViewById(R.id.fp_iv_vip);
        rlAvatar = findViewById(R.id.fp_rl_avatar);
        viewLine = findViewById(R.id.fp_view_line);

        initListener();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fp_rl_user_info:
                if (LoginUtil.isLogin(getContext())) {
                    if (isPersonal) {
                        PersonInfoActivity.start(getContext());
                    }
                }
                break;
            case R.id.fp_tv_return_account:
                if (LoginUtil.isLogin(getContext())) {
                    MyAccountActivity.start(getContext());
                }
                break;
            case R.id.fp_tv_cash_coupon:
                if (LoginUtil.isLogin(getContext())) {
                    String url=accountInfoBean.getCoin_url();
                    if (!FRString.isEmpty(url)) {
                        WebViewActivity.start(getContext(), url);
                    }
                }
                break;
            case R.id.fp_fl_member:
                if (isPersonal) {
                    if (isMember || memberOverdue) {
                        //是会员或者会员已过期，跳转到我的会员页面
                        MyMemberActivity.start(getContext(), accountInfoBean);
                        return;
                    }
                    MemberActivity.start(getContext());
                }
                break;
        }
    }

    private void initListener() {
        rlUserInfo.setOnClickListener(this);
        tvReturnAccount.setOnClickListener(this);
        tvCashCoupon.setOnClickListener(this);
        flMember.setOnClickListener(this);
    }

    private void initTitleBar() {
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
        titleBarUIComponent.initTitle(isPersonal ? "我的" : "购买VIP会员");
        if (isPersonal) {
            titleBarUIComponent.initRight(R.drawable.icon_setting, new OnClickListener() {
                @Override
                public void onClick(View v) {
                    SettingActivity.start(getContext());
                }
            });
        } else {
            titleBarUIComponent.initLeftBack(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((MemberActivity) getContext()).finishActivity();
                }
            });
        }
    }

    public void getUserInfo(BaseImpl baseImpl) {
        PersonUserInfoModel.getInstance().execute(new FRBaseObserver<AccountInfoBean>(baseImpl) {
            @Override
            protected void onBaseNext(AccountInfoBean data) {
                accountInfoBean = data;
                if (!isPersonal) {
                    ((MemberActivity) getContext()).setAccountBean(data);
                }
                setContent(data);
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                setContent(null);
            }
        });
    }

    private void setMemberAvatar() {
        if (isMember) {
            rlAvatar.setBackgroundResource(R.drawable.member_ring_bg);
        } else {
            rlAvatar.setBackground(null);
        }
    }

    private void setMemberContent(int flag, String member) {
        tvMember.setVisibility(View.GONE);
        switch (flag) {
            case 0:
                flMember.setBackgroundResource(R.drawable.icon_member_not_login);
                break;
            case 1:
                flMember.setBackgroundResource(R.drawable.icon_member);
                tvMember.setVisibility(View.VISIBLE);
                tvMember.setText(FRCollection.appendAsString(member, "到期", ""));
                break;
            case 2:
                flMember.setBackgroundResource(R.drawable.icon_member_expire);
                break;
            case 3:
                flMember.setBackgroundResource(R.drawable.icon_not_member);
                break;
        }
    }

    public void setContent(AccountInfoBean data) {
        String returnAccount;
        String coinCenter;
        String accountSwitch;
        String phone;
        rlUserInfo.setVisibility(View.VISIBLE);
        boolean memberSwitch;
        if (null == data) {
            ivAvatar.setImageResource(R.drawable.icon_default_avatar);
            returnAccount = "0.00";
            coinCenter = "0";
            accountSwitch = "0";
            phone = UserDao.getInstance().getPhone();
            memberSwitch = isPersonal;
            isMember = false;
            setMemberContent(0, "");
            setPhone(0, phone, "", "");
        } else {
            returnAccount = data.getReturn_account();
            if (FRString.isEmpty(returnAccount)) {
                returnAccount = "0.00";
            }
            memberSwitch = CommonUtil.isStatusSuccess(data.getMember_switch());
            if (memberSwitch) {
                isMember = CommonUtil.isStatusSuccess(data.getMember_status());
                memberOverdue = !FRString.isEmpty(data.getOverdue_day());
                if (isMember) {
                    ivVip.setVisibility(View.VISIBLE);
                } else {
                    ivVip.setVisibility(View.GONE);
                }
                setMemberContent(data.getMember_status(), data.getMember_day());
                setMemberAvatar();
            }
            coinCenter = data.getCoin_center();
            accountSwitch = data.getAccount_switch();
            if (!FRString.isEmpty(data.getUser_logo())) {
                FRImageLoad.loadCircle(data.getUser_logo(), ivAvatar, R.drawable.icon_default_avatar);
            } else {
                ivAvatar.setImageResource(R.drawable.icon_default_avatar);
            }
            phone = data.getPhone();
            setPhone(data.getMember_status(), phone, data.getMember_day(), data.getOverdue_day());
        }
        viewLine.setVisibility(isPersonal ? View.VISIBLE : View.GONE);
        if (isPersonal) {
            if (memberSwitch) {
                flMember.setVisibility(View.VISIBLE);
            } else {
                flMember.setVisibility(View.GONE);
            }
            llAccount.setVisibility(CommonUtil.isStatusSuccess(accountSwitch) ? VISIBLE : GONE);
            tvReturnAccount.setText(getSpannableContent("返现账户", returnAccount));
            tvCashCoupon.setText(getSpannableContent("金币中心", coinCenter));
        }
    }

    private void setPhone(int memberStatus, String phone, String memberDay, String overdueDay) {
        String content;
        if (!LoginUtil.isLogin()) {
            phone = "未登录";
            if (isPersonal) {
                tvPhone.setText(phone);
                return;
            }
            content = FRCollection.appendAsString(phone, "\n已是会员？登录享受会员权益", "");
        } else {
            if (isPersonal) {
                //不需要显示
                tvPhone.setText(FRString.valueOf(phone));
                return;
            }
            if (isMember) {
                content = FRCollection.appendAsString(phone, "\n", memberDay, "到期", "");
            } else if (memberStatus == 2) {
                content = FRCollection.appendAsString(phone, "\n", "会员离开您已经", overdueDay, "天啦", "");
            } else {
                content = FRCollection.appendAsString(phone, "\n", "您还不是会员，去开通？", "");
            }
        }
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.c999999)), phone.length(), content.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ss.setSpan(new AbsoluteSizeSpan(10, true), phone.length(), content.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvPhone.setText(ss);
    }

    private SpannableString getSpannableContent(String name, String content) {
        String str = FRCollection.appendAsString(name, content, "\n");
        SpannableString ss = new SpannableString(str);
        ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.colorPrimary)), name.length(), str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ss.setSpan(new AbsoluteSizeSpan(16, true), name.length(), str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return ss;
    }

    public void setPersonal(boolean personal) {
        isPersonal = personal;
        initTitleBar();
    }
}